<template>
  <div class="auth-container">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="decoration-circle circle-1"></div>
      <div class="decoration-circle circle-2"></div>
      <div class="decoration-circle circle-3"></div>
    </div>

    <!-- 主内容区 -->
    <div class="auth-content">
      <!-- Logo区域 -->
      <div class="logo-section">
        <div class="logo">
          <i class="fas fa-file-contract logo-icon"></i>
          <div class="logo-text">合规检验智能体</div>
        </div>
        <p class="logo-subtitle">专业、智能、高效的文档合规检验平台</p>
      </div>

      <!-- 认证卡片 -->
      <div class="auth-card">
        <!-- 标签页切换 -->
        <div class="auth-tabs">
          <button 
            :class="['tab-btn', { active: currentTab === 'login' }]"
            @click="switchTab('login')"
          >
            <i class="fas fa-sign-in-alt"></i>
            登录
           </button>
          <!--<button 
            :class="['tab-btn', { active: currentTab === 'register' }]"
            @click="switchTab('register')"
          >
            <i class="fas fa-user-plus"></i>
            注册
          </button> -->
        </div>

        <!-- 表单内容 -->
        <div class="form-container">
          <!-- 登录表单 -->
          <form v-if="currentTab === 'login'" class="auth-form" @submit="handleLogin">
            <div class="form-group">
              <label class="form-label">
                <i class="fas fa-mobile-alt"></i>
                手机号码
              </label>
              <input
                v-model="loginForm.phoneNumber"
                type="text"
                class="form-input"
                :class="{ error: errors.phoneNumber }"
                maxlength="11"
                placeholder="请输入手机号码"
                @blur="validateField('phoneNumber')"
                @input="clearError('phoneNumber')"
              />
              <div v-if="errors.phoneNumber" class="error-message">
                <i class="fas fa-exclamation-circle"></i>
                {{ errors.phoneNumber }}
              </div>
            </div>

            <div class="form-group">
              <label class="form-label">
                <i class="fas fa-lock"></i>
                密码
              </label>
              <div class="password-input">
                <input
                  v-model="loginForm.password"
                  :type="showPassword ? 'text' : 'password'"
                  class="form-input"
                  :class="{ error: errors.password }"
                  placeholder="请输入密码"
                  @blur="validateField('password')"
                  @input="clearError('password')"
                />
                <button
                  type="button"
                  class="password-toggle"
                  @click="showPassword = !showPassword"
                >
                  <i :class="showPassword ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
                </button>
              </div>
              <div v-if="errors.password" class="error-message">
                <i class="fas fa-exclamation-circle"></i>
                {{ errors.password }}
              </div>
              
              <!-- 登录错误信息 -->
              <div v-if="errors.loginError" class="login-error-message">
                <i class="fas fa-exclamation-triangle"></i>
                {{ errors.loginError }}
              </div>
            </div>

            <div class="form-options">
              <label class="checkbox-label">
                <input v-model="loginForm.remember" type="checkbox" class="checkbox">
                <span class="checkbox-custom"></span>
                记住我
              </label>
              <a href="#" class="forgot-link">忘记密码？</a>
            </div>

            <button type="submit" class="submit-btn" :disabled="isLoading">
              <i v-if="isLoading" class="fas fa-spinner fa-spin"></i>
              <i v-else class="fas fa-sign-in-alt"></i>
              {{ isLoading ? '登录中...' : '登录' }}
            </button>
          </form>

          <!-- 注册表单 -->
          <form v-if="currentTab === 'register'" class="auth-form" @submit.prevent>
            <div class="form-row">
              <div class="form-group">
                <label class="form-label">
                  <i class="fas fa-user"></i>
                  姓名
                </label>
                <input
                  v-model="registerForm.fullName"
                  type="text"
                  class="form-input"
                  :class="{ error: errors.fullName }"
                  placeholder="请输入真实姓名"
                  @blur="validateField('fullName')"
                  @input="clearError('fullName')"
                />
                <div v-if="errors.fullName" class="error-message">
                  <i class="fas fa-exclamation-circle"></i>
                  {{ errors.fullName }}
                </div>
              </div>

              <div class="form-group">
                <label class="form-label">
                  <i class="fas fa-mobile-alt"></i>
                  手机号码
                </label>
                <input
                  v-model="registerForm.phoneNumber"
                  type="text"
                  class="form-input"
                  :class="{ error: errors.regPhoneNumber }"
                  placeholder="请输入手机号码"
                  @blur="validateField('regPhoneNumber')"
                  @input="clearError('regPhoneNumber')"
                />
                <div v-if="errors.regPhoneNumber" class="error-message">
                  <i class="fas fa-exclamation-circle"></i>
                  {{ errors.regPhoneNumber }}
                </div>
              </div>
            </div>

            <div class="form-group">
              <label class="form-label">
                <i class="fas fa-envelope"></i>
                邮箱
              </label>
              <input
                v-model="registerForm.email"
                type="email"
                class="form-input"
                :class="{ error: errors.email }"
                placeholder="请输入邮箱地址"
                @blur="validateField('email')"
                @input="clearError('email')"
              />
              <div v-if="errors.email" class="error-message">
                <i class="fas fa-exclamation-circle"></i>
                {{ errors.email }}
              </div>
            </div>

            <div class="form-row">
              <div class="form-group">
                <label class="form-label">
                  <i class="fas fa-lock"></i>
                  密码
                </label>
                <div class="password-input">
                  <input
                    v-model="registerForm.password"
                    :type="showRegPassword ? 'text' : 'password'"
                    class="form-input"
                    :class="{ error: errors.regPassword }"
                    placeholder="请输入密码"
                    @blur="validateField('regPassword')"
                    @input="clearError('regPassword')"
                  />
                  <button
                    type="button"
                    class="password-toggle"
                    @click="showRegPassword = !showRegPassword"
                  >
                    <i :class="showRegPassword ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
                  </button>
                </div>
                <div v-if="errors.regPassword" class="error-message">
                  <i class="fas fa-exclamation-circle"></i>
                  {{ errors.regPassword }}
                </div>
              </div>

              <div class="form-group">
                <label class="form-label">
                  <i class="fas fa-lock"></i>
                  确认密码
                </label>
                <div class="password-input">
                  <input
                    v-model="registerForm.confirmPassword"
                    :type="showConfirmPassword ? 'text' : 'password'"
                    class="form-input"
                    :class="{ error: errors.confirmPassword }"
                    placeholder="请再次输入密码"
                    @blur="validateField('confirmPassword')"
                    @input="clearError('confirmPassword')"
                  />
                  <button
                    type="button"
                    class="password-toggle"
                    @click="showConfirmPassword = !showConfirmPassword"
                  >
                    <i :class="showConfirmPassword ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
                  </button>
                </div>
                <div v-if="errors.confirmPassword" class="error-message">
                  <i class="fas fa-exclamation-circle"></i>
                  {{ errors.confirmPassword }}
                </div>
              </div>
            </div>

            <button type="submit" class="submit-btn" :disabled="isLoading || !registerForm.agreeTerms">
              <i v-if="isLoading" class="fas fa-spinner fa-spin"></i>
              <i v-else class="fas fa-user-plus"></i>
              {{ isLoading ? '注册中...' : '立即注册' }}
            </button>
          </form>
        </div>
      </div>

      <!-- 页脚信息 -->
      <div class="footer-info">
        <p>&copy; 先途科技. 保留所有权利.</p>
        <p class="footer-links">
          <a href="#">关于我们</a>
          <span>|</span>
          <a href="#">帮助中心</a>
          <span>|</span>
          <a href="#">联系我们</a>
        </p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useAuth } from '../scripts/auth/auth'

// 使用认证相关的逻辑
const {
  currentTab,
  isLoading,
  showPassword,
  showRegPassword,
  showConfirmPassword,
  loginForm,
  registerForm,
  errors,
  switchTab,
  clearError,
  validateField,
  handleLogin
} = useAuth()

</script>

<style>
.auth-container {
  min-height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
}

.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.decoration-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.circle-1 {
  width: 200px;
  height: 200px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.circle-2 {
  width: 300px;
  height: 300px;
  top: 60%;
  right: 10%;
  animation-delay: 2s;
}

.circle-3 {
  width: 150px;
  height: 150px;
  bottom: 20%;
  left: 30%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-20px);
  }
}

.auth-content {
  width: 100%;
  max-width: 450px;
  padding: 2rem;
  position: relative;
  z-index: 1;
}

.logo-section {
  text-align: center;
  margin-bottom: 2rem;
}

.logo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  margin-bottom: 1rem;
}

.logo-icon {
  font-size: 3rem;
  color: var(--light);
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.2));
}

.logo-text {
  font-size: 2rem;
  font-weight: 700;
  color: var(--light);
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.logo-subtitle {
  color: rgba(255, 255, 255, 0.9);
  font-size: 1rem;
  font-weight: 300;
}

.auth-card {
  background: var(--light);
  border-radius: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  backdrop-filter: blur(10px);
}

.auth-tabs {
  display: flex;
  background: var(--gray);
}

.tab-btn {
  flex: 1;
  padding: 1.2rem;
  border: none;
  background: none;
  cursor: pointer;
  font-weight: 600;
  color: var(--text-secondary);
  transition: var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  position: relative;
}

.tab-btn.active {
  color: var(--primary);
  background: var(--light);
}

.tab-btn.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background: var(--primary);
}

.form-container {
  padding: 2rem;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.form-group {
  position: relative;
}

.form-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: var(--text-primary);
  font-size: 0.9rem;
}

.form-label i {
  color: var(--primary);
  width: 16px;
}

.form-input {
  width: 100%;
  padding: 1rem;
  border: 2px solid var(--border);
  border-radius: 10px;
  font-size: 1rem;
  transition: var(--transition);
  background: var(--light);
}

.form-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.1);
}

.form-input.error {
  border-color: var(--error);
}

.password-input {
  position: relative;
}

.password-toggle {
  position: absolute;
  right: 1rem;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 5px;
  transition: var(--transition);
}

.password-toggle:hover {
  color: var(--primary);
  background: rgba(67, 97, 238, 0.1);
}

.error-message {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--error);
  font-size: 0.85rem;
  margin-top: 0.5rem;
}

.login-error-message {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--error);
  font-size: 0.9rem;
  margin-bottom: 1rem;
  padding: 0.75rem;
  background-color: rgba(255, 0, 0, 0.05);
  border-radius: 8px;
  border-left: 3px solid var(--error);
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.checkbox {
  display: none;
}

.checkbox-custom {
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-radius: 4px;
  position: relative;
  transition: var(--transition);
}

.checkbox:checked + .checkbox-custom {
  background: var(--primary);
  border-color: var(--primary);
}

.checkbox:checked + .checkbox-custom::after {
  content: '✓';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
  font-size: 12px;
  font-weight: bold;
}

.forgot-link, .terms-link {
  color: var(--primary);
  text-decoration: none;
  font-size: 0.9rem;
  transition: var(--transition);
}

.forgot-link:hover, .terms-link:hover {
  color: var(--secondary);
  text-decoration: underline;
}

.submit-btn {
  width: 100%;
  padding: 1.2rem;
  background: linear-gradient(135deg, var(--primary), var(--secondary));
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  box-shadow: 0 8px 20px rgba(67, 97, 238, 0.3);
}

.submit-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 12px 25px rgba(67, 97, 238, 0.4);
}

.submit-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

.footer-info {
  text-align: center;
  margin-top: 2rem;
  color: rgba(255, 255, 255, 0.8);
  font-size: 0.9rem;
}

.footer-links {
  margin-top: 0.5rem;
}

.footer-links a {
  color: rgba(255, 255, 255, 0.9);
  text-decoration: none;
  margin: 0 0.5rem;
  transition: var(--transition);
}

.footer-links a:hover {
  color: white;
}

.footer-links span {
  margin: 0 0.5rem;
  opacity: 0.5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .auth-content {
    padding: 1rem;
    max-width: 100%;
  }
  
  .logo-text {
    font-size: 1.5rem;
  }
  
  .logo-icon {
    font-size: 2.5rem;
  }
  
  .form-container {
    padding: 1.5rem;
  }
  
  .form-row {
    grid-template-columns: 1fr;
  }
  
  .form-options {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }
}

@media (max-width: 480px) {
  .auth-content {
    padding: 0.5rem;
  }
  
  .auth-card {
    border-radius: 15px;
  }
  
  .tab-btn {
    padding: 1rem;
    font-size: 0.9rem;
  }
  
  .logo {
    flex-direction: column;
    gap: 0.5rem;
  }
}
</style>
